package com.dds.web.controller.union;

import com.dds.common.core.controller.BaseController;
import com.dds.common.core.domain.AjaxResult;
import com.dds.common.core.page.TableDataInfo;
import com.dds.common.utils.ServletUtils;
import com.dds.framework.web.service.TokenService;
import com.dds.system.domain.union.UnionShopVerifyFlow;
import com.dds.system.service.union.IUnionShopBusInfoService;
import com.dds.system.service.union.IUnionShopContractService;
import com.dds.system.service.union.IUnionShopIndexService;
import com.dds.system.service.union.IUnionShopVerifyFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/union/shopExamine")
public class UnionShopExamineController extends BaseController {
    @Autowired
    private IUnionShopIndexService unionShopIndexService;

    @Autowired
    private IUnionShopBusInfoService unionShopBusInfoService;

    @Autowired
    private IUnionShopContractService unionShopContractService;

    @Autowired
    private IUnionShopVerifyFlowService unionShopVerifyFlowService;

    @Autowired
    private TokenService tokenService;

    @PreAuthorize("@ss.hasPermi('union:shopExamine:list')")
    @GetMapping("/list")
    public TableDataInfo list(
            @RequestParam(name = "type", required = false, defaultValue = "1") String type,
            @RequestParam(name = "shopName", required = false) String shopName,
            @RequestParam(name = "serverName", required = false) String serverName,
            @RequestParam(name = "corpName", required = false) String corpName,
            @RequestParam(name = "legalPersonName", required = false) String legalPersonName
    ) {
        startPage();

        List<Map<String, Object>> list = unionShopIndexService.selectExamineList(
                type, shopName, serverName, corpName, legalPersonName
        );

        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('union:shopExamine:info')")
    @GetMapping(value = "/info/{shopId}")
    public AjaxResult info(@PathVariable("shopId") String shopId) {
        return AjaxResult.success(unionShopIndexService.selectUnionShopIndexByIdForExamine(shopId));
    }

    @GetMapping(value = "/getContract/{shopId}")
    public AjaxResult getContract(@PathVariable("shopId") String shopId) {
        return AjaxResult.success(unionShopContractService.selectUnionShopContractByShopId(shopId));
    }

    @GetMapping(value = "/getVerifyLog")
    public TableDataInfo getVerifyLog(
            @RequestParam(name = "shopId") String shopId
    ) {
        startPage();

        List<Map<String, Object>> unionShopVerifyFlows = unionShopVerifyFlowService.selectUnionShopVerifyFlowList(shopId);

        return getDataTable(unionShopVerifyFlows);
    }

    @PreAuthorize("@ss.hasPermi('union:shopExamine:toExamine')")
    @PostMapping("/toExamine")
    public AjaxResult toExamine(@RequestBody String params) {
        Map<String, Object> data = unionShopIndexService.examineShop(params, tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());

        if (data.get("status").equals(true)) {
            return AjaxResult.success(data.get("msg").toString());
        } else {
            return AjaxResult.error(data.get("msg").toString());
        }
    }
}
